package com.fox.hiring.company.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fox.hiring.common.DBObject;
import com.fox.hiring.common.util.DBUtil;
import com.fox.hiring.company.entity.PositionEntity;
import com.fox.hiring.talent.entity.ResumeEntity;

/**
 * Position Dao
 * 
 * @author fox.
 * @since 2016年2月26日
 */
public class PositionDao {
	
	/**
	 * 显示当前企业发布的所有职位信息
	 * 
	 * @param talentId
	 * @return
	 */
	public List<PositionEntity> list(int compnayId) {
		List<PositionEntity> positionList = new ArrayList<>();
		String sql = "SELECT * FROM position WHERE company_id=?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, compnayId);
			ResultSet resultSet = dbObject.getResultSet();
			while (resultSet.next()) {
				PositionEntity position = new PositionEntity();
				position.setId(resultSet.getInt("id"));
				position.setName(resultSet.getString("name"));
				position.setDuty(resultSet.getString("duty"));
				position.setAbility(resultSet.getString("ability"));
				position.setSalary1(resultSet.getInt("salary1"));
				position.setSalary2(resultSet.getInt("salary2"));
				position.setReleaseTime(resultSet.getTimestamp("Release_time"));
				positionList.add(position);
			}
		} catch (Exception e) {
			// resumeList = new ArrayList<>();
			positionList = Collections.emptyList();
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return positionList;
	}


	/**
	 * 显示全部的职位信息
	 * 
	 * @return
	 */
	public List<PositionEntity> listAllPosition() {
		List<PositionEntity> positionList = new ArrayList<>();
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT p.id, c.`name` AS 'company_name', c.address, c.slogan, p.`name` AS 'position_name', p.salary1, p.salary2, p.release_time ");
		sb.append("FROM position p, company c ");
		sb.append("WHERE p.company_id = c.id");
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sb.toString());
			ResultSet resultSet = dbObject.getResultSet();
			while (resultSet.next()) {
				PositionEntity position = new PositionEntity();
				position.setId(resultSet.getInt("id"));
				position.setCompanyName(resultSet.getString("company_name"));
				position.setCompanyAddress(resultSet.getString("address"));
				position.setCompanySlogan(resultSet.getString("slogan"));
				position.setName(resultSet.getString("position_name"));
				position.setSalary1(resultSet.getInt("salary1"));
				position.setSalary2(resultSet.getInt("salary2"));
				// 数据库存储：2016-02-26 08:57:36
				// resultSet.getDate("release_time"); -> 2016-02-26
				// resultSet.getTime("release_time"); -> 08:57:36
				// resultSet.getTime("release_time"); -> 2016-02-26 08:57:36
				position.setReleaseTime(resultSet.getTimestamp("release_time"));
				positionList.add(position);
			}
		} catch (Exception e) {
			positionList = new ArrayList<>();
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return positionList;
	}

	/**
	 * 查询职位的详细信息
	 * 
	 * @param positionId
	 * @return
	 */
	public PositionEntity get(int positionId) {
		PositionEntity position = null;
		DBObject dbObject = null;
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ");
		sql.append("p.id, p.name, p.duty, p.ability, p.salary1, p.salary2, p.release_time, ");
		sql.append("c.name AS 'company_name', c.linkman, c.phone, c.email, c.numbers, c.slogan, c.address ");
		sql.append("FROM position p, company c ");
		sql.append("WHERE p.company_id=c.id ");
		sql.append("AND p.id=?");
		try {
			dbObject = DBUtil.executeQuery(sql.toString(), positionId);
			ResultSet resultSet = dbObject.getResultSet();
			if (resultSet.next()) {
				position = new PositionEntity();
				position.setId(resultSet.getInt("id"));
				position.setName(resultSet.getString("name"));
				position.setDuty(resultSet.getString("duty"));
				position.setAbility(resultSet.getString("ability"));
				position.setSalary1(resultSet.getInt("salary1"));
				position.setSalary2(resultSet.getInt("salary2"));
				position.setReleaseTime(resultSet.getTimestamp("release_time"));
				position.setCompanyName(resultSet.getString("company_name"));
				position.setCompanyLinkman(resultSet.getString("linkman"));
				position.setCompanyPhone(resultSet.getString("phone"));
				position.setCompanyEmail(resultSet.getString("email"));
				position.setCompanyNumbers(resultSet.getString("numbers"));
				position.setCompanySlogan(resultSet.getString("slogan"));
				position.setCompanyAddress(resultSet.getString("address"));
			}
		} catch (SQLException e) {
			position = null;
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return position;
	}
/**
 * 发布职位
 * 
 * @param position
 * @return
 */

	public int add(PositionEntity position) {
		int row = 0;
		String sql="INSERT INTO position VALUES(NULL, ?, ?, ?, ?, ?, ?, NOW())";
		try {
			List<Object> paramList = new ArrayList<>();
			paramList.add(position.getCompanyId());
			paramList.add(position.getName());
			paramList.add(position.getDuty());
			paramList.add(position.getAbility());
			paramList.add(position.getSalary1());
			paramList.add(position.getSalary2());
			row = DBUtil.executeUpdate(sql, paramList);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}
/**
 * 职位删除
 * 
 * @param positionId
 * @return
 */

public int deletePosition(String positionId) {
	int row = 0;
	String sql="DELETE  FROM position WHERE id=?";
	try {
		 row = DBUtil.executeUpdate(sql, positionId);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return row;
}


public int update(PositionEntity position) {
	int row = 0;
	String sql="UPDATE position SET name=?, duty=?, ability=?, salary1=?, salary2=? WHERE id=?";
	try {
		List<Object> paramList = new ArrayList<>();
		paramList.add(position.getName());
		paramList.add(position.getDuty());
		paramList.add(position.getAbility());
		paramList.add(position.getSalary1());
		paramList.add(position.getSalary2());
		paramList.add(position.getId());
		row = DBUtil.executeUpdate(sql, paramList);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	return row;
}

}

